﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for EditableListBoxItem.xaml
    /// </summary>
    public partial class EditableListBoxItem : UserControl, INotifyPropertyChanged
    {
        public EditableListBoxItem() {
            InitializeComponent();
            //DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #region Dependency Properties
        public static readonly DependencyProperty PrefixProperty = DependencyProperty.Register("Prefix",
            typeof(string), typeof(EditableListBoxItem), new PropertyMetadata(PrefixChanged));

        public string Prefix {
            get {
                return (string)GetValue(PrefixProperty);
            }
            set {
                SetValue(PrefixProperty, value);
                NotifyPropertyChanged("Prefix");
            }
        }

        private static void PrefixChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
            //TOOD: Update UI
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
            typeof(string), typeof(EditableListBoxItem), new PropertyMetadata(ValueChanged));

        public string Value {
            get {
                return (string)GetValue(ValueProperty);
            }
            set {
                SetValue(ValueProperty, value);
                NotifyPropertyChanged("Value");
            }
        }

        private static void ValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
            //TOOD: Update UI
        }
        #endregion


        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            //Make the TextBox editable and put the focus there.
            Console.Write("BreakPoint");

            //Keyboard.Focus(ValueTextBox);
            ValueTextBox.SelectAll();
            ValueTextBox.IsEnabled = true;
            ValueTextBox.Focus();
            Keyboard.Focus(ValueTextBox); 
        }

        private void ValueTextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
            //Console.WriteLine("Lost Keyboard focus");
            //ValueTextBox.IsEnabled = false;   
        }

        private void UserControl_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
            if (!(this.IsKeyboardFocusWithin)) {
                ValueTextBox.IsEnabled = false;
            }
        }
    }
}
